package com.whl.sm.service.impl.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whl.sm.mapper.system.MenuCpRoleMapper;
import com.whl.sm.model.system.MenuCpRole;
import com.whl.sm.service.system.MenuCpRoleService;
import com.whl.sm.service.system.OrgUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wuhaolun
 * @since 2021-09-10
 */
@Service
public class MenuCpRoleServiceImpl extends ServiceImpl<MenuCpRoleMapper, MenuCpRole> implements MenuCpRoleService {

    @Autowired
    MenuCpRoleMapper menuCpRoleMapper;

    @Autowired
    private OrgUserService orgUserService;

    @Override
    public List<String> getMenusByRelateRoleId(String roleId) {
        List<String> menuIdList =  menuCpRoleMapper.getMenusByRelateRoleId(roleId);

        return menuIdList;
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED)
    public String editRoleMenuInfo(String roleId, List<String> menuIdList) {
        menuCpRoleMapper.delete(new QueryWrapper<MenuCpRole>().eq("role_id",roleId));
        MenuCpRole mcp;
        for (int i = 0; i < menuIdList.size(); i++) {
            mcp = new MenuCpRole();
            mcp.setOperator(orgUserService.getCurrentUserId());
            mcp.setMenuId(menuIdList.get(i));
            mcp.setRoleId(roleId);
            mcp.setOperator("sys");
            menuCpRoleMapper.insert(mcp);
        }
        return "success";
    }


}
